/*!
 * jQuery GoSearch Plugin v1.0.1
 *
 * Copyright Harry Han
 * Released under the MIT
 *
 * Date: 2013-04-17
 */
(function ($) {

    $.fn.goSearch = function (options) {

        var defaults = {
            val: function () { return ''; },
            theme: 'jq_gosearch',
            id: "jq_gosearch",
            enableFuzzy: true, //启用模糊/精确切换
            isFuzzy: true, //是否模糊
            enableColumnToRow: true, //启用列转行
            isColumnToRow: false,
            enableChoosePanel: true,
            splitChar: ',', //分割符
            onSearch: function () { } //搜索事件
        };

        options = $.extend(defaults, options);

        this.each(function () {
            var src = $(this);

            //构造HTML Tree
            var htm = [];
            htm.push('<input name="" type="text" class="' + options.theme + '_p1" id="' + options.id + '" />');
            htm.push('<span class="' + options.theme + '_p2 k-button k-button-icontext">搜索</span>');
            if (options.enableFuzzy)
                htm.push('<span class="' + options.theme + '_p3 k-button k-button-icontext">' + (options.isFuzzy ? '模糊' : '精确') + '</span>');
            if (options.enableColumnToRow) {
                htm.push('<span class="' + options.theme + '_p4 k-button k-button-icontext">批量搜索<i class="k-icon k-i-arrow-s"></i></span>');
                htm.push('<div class="' + options.theme + '_search" style="display:none">');
                htm.push('<textarea></textarea>');
                htm.push('<div class="' + options.theme + '_search_div">');
                htm.push('<input type="button" class="k-button k-button-icontext" value="确定"> <input type="button" class="k-button k-button-icontext"  value="清空"> <input type="button" class="k-button k-button-icontext"  value="取消"></div></div>');
            }
            if (options.enableChoosePanel) {
                htm.push('<p class="' + options.theme + '_p3"><span><b>您已经选择：</b></span></p>');
            }
            htm.push('<div class="clear"></div>');
            src.html('').append(htm.join(''));

            options.val = function () {
                return $(':text:first', src).val();
            }

            //回车查询+字符修正
            $(':text:first', src).keyup(function (e) {
                if (e.which >= 35 && e.which <= 40) return;
                //这段语句会造成chrome中文输入法异常,详情见如下链接
                //0.关键字:keypress keydown keyup 输入法
                //1.http://itindex.net/detail/14431-jquery-%E4%B8%AD%E6%96%87-keydown
                //2.http://www.cnblogs.com/qhca/archive/2011/12/12/2284858.html
                //var txt = $(this).val().replace(/[，、]/g, ',');
                //$(this).val(txt);
                if (e.which == 13) options.onSearch();
            });

            //绑定查询函数
            $('.' + options.theme + '_p2', src).click(options.onSearch);

            //模糊/精确切换
            if (options.enableFuzzy) {
                $('.' + options.theme + '_p3', src).click(function () {
                    options.isFuzzy = !options.isFuzzy;
                    $(this).text(options.isFuzzy ? '模糊' : '精确');
                    goTextEnd($(':text:first', src));
                });
            }

            //快速查询
            if (options.enableColumnToRow) {
                //点击展开/关闭
                $('.' + options.theme + '_p4', src).click(function () {
                    options.isColumnToRow = !options.isColumnToRow;
                    $('.' + options.theme+"_search", src).css('display', options.isColumnToRow ? 'block' : 'none');
                    if (options.isColumnToRow)
                        goTextEnd($('.' + options.theme+'_search textarea', src));
                    else {
                        $('.' + options.theme+'_search textarea', src).text('');
                        goTextEnd($(':text:first', src));
                    }
                });

                //确定搜索按钮
                $('.' + options.theme+'_search :button:eq(0)').click(function () {
                    var convertText = ColumnToRow($('.' + options.theme+'_search textarea', src).val(), options.splitChar);
                    $(':text:first', src).val(convertText);
                    options.isColumnToRow = false;
                    $('.' + options.theme+'_search', src).css('display', 'none');
                    //$('.' + options.theme+'_search textarea', src).text('');
                    goTextEnd($(':text:first', src));
                    options.onSearch();
                });

                //清空按钮
                $('.' + options.theme+'_search :button:eq(1)').click(function () {
                    $('.' + options.theme+'_search textarea', src).text('');
                    goTextEnd($('.' + options.theme+'_search textarea', src));
                });

                //取消按钮
                $('.' + options.theme+'_search :button:eq(2)').click(function () {
                    options.isColumnToRow = false;
                    $('.' + options.theme+'_search', src).css('display', 'none');
                    $('.' + options.theme+'_search textarea', src).text('');
                    goTextEnd($(':text:first', src));
                });
            }

        });

        function ColumnToRow(c, splitChar) {
            var re = /[\n]+/ig;
            return $.trim(c.replace(eval('/[' + splitChar + ']/g'), ''), '\n').replace(re, splitChar);
        }

        function goTextEnd(obj) {
            obj.focus();
            /*//非ie浏览器不支持
            with (document.selection.createRange()) { 
                moveStart("character", obj.val().length);
                collapse();
                select();
            }*/
        }
        return options;
    };
})(jQuery);